<?php
namespace app\common\logic;

use app\common\model\UserBonusDetail;
use think\Controller;

class Shareholder extends Controller
{
    public function get_list($userid,$start_time="",$end_time="",$start=1,$num=20){

        $where=[];
        if($start_time){
            $where['o.bonus_time']=['egt',$start_time];
        }

        if($end_time){
            $where['o.bonus_time']=['elt',$end_time];
        }

        if($start_time&&$end_time){
            $where['o.bonus_time']=['between',[$start_time,$end_time]];
        }

        return UserBonusDetail::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->where($where)
            ->where("o.user_id",$userid)
            ->page($start,$num)
            ->order("o.bonus_time desc")
            ->field("s.name,o.*")->select();



    }

    public function get_shop_list($shopid,$start_time="",$end_time="",$start=1,$num=20,$username=''){

        $where=[];
        if($start_time){
            $where['o.bonus_time']=['egt',$start_time];
        }

        if($end_time){
            $where['o.bonus_time']=['elt',$end_time];
        }

        if($start_time&&$end_time){
            $where['o.bonus_time']=['between',[$start_time,$end_time]];
        }

        if($username){
            $where['u.username']=['like','%'.$username.'%'];
        }

        $data= UserBonusDetail::alias('o')
            ->join('t_user u','o.shop_id = u.id','LEFT')
            ->where($where)
            ->where("o.shop_id",$shopid)
            ->page($start,$num)
            ->order("o.bonus_time desc")
            ->field("u.username,o.*")->select();

        $count=UserBonusDetail::alias('o')
            ->join('t_user u','o.shop_id = u.id','LEFT')
            ->where("o.shop_id",$shopid)
            ->page($start,$num)
            ->group("o.user_id")
            ->field("u.username,o.*")->count();

        return [$data,$count];
    }
}